<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/4 0004
 * Time: 15:53
 */

namespace Admin\Action;


class PackageAction extends BaseAction{

    /**
     * 选择商家
     */


    //第一版  分布操作代码
//    public function addPackage(){
//        $this->isLogin();
//       // $this->checkPrivelege('dplb_00');
//        //获取地区信息
//        $m = D('Admin/Areas');
//        $this->assign('areaList',$m->queryShowByList(0));
//        $m = D('Admin/Shops');
//        $page = $m->queryByPage();
//        $pager = new \Think\Page($page['total'],$page['pageSize'],I());// 实例化分页类 传入总记录数和每页显示的记录数
//        $page['pager'] = $pager->show(1);
//
//        $this->assign('Page',$page);
//        $this->assign('shopName',I('shopName'));
//        $this->assign('shopSn',I('shopSn'));
//        $this->assign('areaId1',I('areaId1',0));
//        $this->assign('areaId2',I('areaId2',0));
//        $this->display("package/addPackage");
//    }


    //改版集成一个页面代码
    public function addPackage(){
        $this->isLogin();
        // $this->checkPrivelege('dplb_00');
        //获取地区信息
        $shopCatList = M("GoodsCats")->where(array("parentId"=>0,"isShow"=>1))->field("catId,catName")->select();
        $this->assign("shopCatList",$shopCatList);
        $this->assign("packageListUrl",U("Admin/Package/index"));
        if(I("get.id")){
           $package =  M("Taocans")->where(array('id'=>I("get.id")))->field("id,tName,desc,price,starttime,endtime")->find();
            if($package){
                $m = M("TaocanGoods");
                $goodsList = $m->join("join wst_goods on wst_taocan_goods.goodsId = wst_goods.goodsId")
                    ->join("join wst_shops on wst_goods.shopId = wst_shops.shopId")
                    ->join("left join wst_price on wst_taocan_goods.periodId = wst_price.priceId")
                    ->field("goodsName,wst_taocan_goods.goodsId,goodsThums,shopName,shopPrice,tPrice,goodsSpec,periodId,price as pPrice,periodStart,periodEnd,isTom,weekDate")
                    ->where(array('tId'=>I("get.id")))
                    ->select();
               $this->assign("goodsList",$goodsList);
                $this->assign("package",$package);
            }
        }





        $this->display("package/addPackage");
    }

    /**
     * 根据商家获取商品
     */
//第一版  分布操作代码
//    public function getGoodsListByShopId(){
//        $shopId = I("get.shopId");
//        $goodsName = I("get.goodsName");
//        $m = M("goods");
//        $m->where(array("shopId"=>$shopId,"goodsStatus"=>1,"goodsFlag"=>1,"isSale"=>1));// 查询满足要求的总记录数
//        if($goodsName){
//            $m->where(array("goodsName"=>array("like","%$goodsName%")));
//        }
//        $count = $m->count();
//        $pageSize = C('PAGE_SIZE');
//        $Page = new \Think\Page($count, $pageSize);
//        $show = $Page->show(1);// 分页显示输出
//        $m->join("wst_shops on wst_shops.shopId = wst_goods.shopId")->join("wst_goods_cats on wst_shops.goodsCatId1 = wst_goods_cats.catId")
//            ->where(array("wst_goods.shopId"=>$shopId,"goodsStatus"=>1,"goodsFlag"=>1,"isSale"=>1));// 查询满足要求的总记录数
//        if($goodsName){
//            $m->where(array("goodsName"=>array("like","%$goodsName%")));
//        }
//        $list = $m->field("goodsId,goodsName,goodsSn,goodsThums,shopPrice,wst_shops.shopName,wst_goods_cats.catName,goodsSpec")
//            ->limit($Page->firstRow.','.$Page->listRows)->select();
//
//        $data["data"] = $list?$list:array();
//        $data["page"] = $show;
//        // $count = $m->where(array("shopId"=>$shopId,"goodsStatus"=>1,"goodsFlag"=>1,"isSale"=>1))->count();
//        $this->ajaxReturn($data);
//    }

   //改版后代码
    public function getGoodsListByShopId(){
        $shopName = I("post.shopName");
        $goodsName = I("post.goodsName");
        $shopCat = I("post.shopCat");
        $m = M("goods");
        $where = array("goodsStatus"=>1,"goodsFlag"=>1,"isSale"=>1);
        if($goodsName){
            $where["goodsName"] = array("like","%$goodsName%");
        }

        if($shopName){
            $where["shopName"] = array("like","%$shopName%");
        }
        if($shopCat){
            $where["catId"] = $shopCat;
        }
        $count = $m->join("wst_shops on wst_shops.shopId = wst_goods.shopId")->join("wst_goods_cats on wst_shops.goodsCatId1 = wst_goods_cats.catId")
            ->where($where)->count('wst_goods.goodsId');
        $pageSize = C('PAGE_SIZE');
        $Page = new \Think\Page($count, $pageSize);
        $show = $Page->show(1);// 分页显示输出
        $list = $m->join("wst_shops on wst_shops.shopId = wst_goods.shopId")->join("wst_goods_cats on wst_shops.goodsCatId1 = wst_goods_cats.catId")
            ->where($where)->field("goodsId,goodsName,goodsSn,goodsThums,shopPrice,wst_shops.shopName,wst_goods_cats.catName,goodsSpec")
            ->limit($Page->firstRow.','.$Page->listRows)->select();

        $data["data"] = $list?$list:array();
        $data["page"] = $show;
        // $count = $m->where(array("shopId"=>$shopId,"goodsStatus"=>1,"goodsFlag"=>1,"isSale"=>1))->count();
        $this->ajaxReturn($data);
    }



    public function getShopListAjax(){
        $areaId1 = I("post.areaId1");
        $areaId2 = I("post.areaId2");
        $shopName = I("post.shopName");
        $shopSn = I("post.shopSn");

        $m = M("Shops");
        if($areaId1){
            $m->where(array("areaId1"=>$areaId1));
        }
        if($areaId2){
            $m->where(array("areaId2"=>$areaId2));
        }
        if($shopName){
            $m->where(array("shopName"=>array("like","%$shopName%")));
        }

        if($shopSn){
            $m->where(array("shopSn"=>array("like","%$shopSn%")));
        }

        $count = $m->count();
        $pageSize = C('PAGE_SIZE');
        $Page = new \Think\Page($count, $pageSize);
        $show = $Page->show(1);// 分页显示输出

        if($areaId1){
            $m->where(array("areaId1"=>$areaId1));
        }
        if($areaId2){
            $m->where(array("areaId2"=>$areaId2));
        }
        if($shopName){
            $m->where(array("shopName"=>array("like","%$shopName%")));
        }

        if($shopSn){
            $m->where(array("shopSn"=>array("like","%$shopSn%")));
        }
        $m->where(array("shopStatus"=>1,"shopFlag"=>1));

        $list = $m->field("shopId,shopName,shopSn,userName,catName")->join("wst_goods_cats on wst_goods_cats.catId = wst_shops.goodsCatId1")->
        join("wst_users on wst_users.userId = wst_shops.userId")->limit($Page->firstRow.','.$Page->listRows)->select();

        $data["data"] = $list?$list:array();
        $data["page"] = $show;
        $this->ajaxReturn($data);

    }


    /**
     * 提交套餐数据
     */

    public function addPackageData()
    {

        $data["status"] = 0;
        $packageId = I("post.packageId");
        $goodsList = I("post.goodsList");
        if (count($goodsList) == 0) {
            $data["msg"] = "套餐商品不能为空";
            $this->ajaxReturn($data);
        }

        $startTime = I("post.tStartTime");
        $endTime = I("post.tEndTime");
        if($startTime == '' || $endTime == ''){
            $data["msg"] = "套餐开始时间或结束时间不能空";
            $this->ajaxReturn($data);
        }

        if($endTime <= $startTime){
            $data["msg"] = "套餐开始时间不能晚于结束时间";
            $this->ajaxReturn($data);
        }

        $tName = I("post.tName");
        if(empty($tName)){
            $data["msg"] = "套餐名称不能空";
            $this->ajaxReturn($data);
        }


        //如果是添加套餐，直接计算总价
        $tTotalPrice = 0;
        foreach ($goodsList as $goods) {
            if (!$goods["tPrice"] || !is_numeric($goods["tPrice"]) || (float)$goods["tPrice"] > (float)$goods["goodsPrice"]) {
                $data["msg"] = "商品套餐价格必须是比商品价格少的整数";
                $this->ajaxReturn($data);
            }
            $tTotalPrice += $goods["tPrice"];
        }






        $data["msg"] = "添加套餐错误";

        $m = M("taocans");
        $gm = M("TaocanGoods");

        $flag = false;

        $m->startTrans();



        $package["tName"] = I("post.tName");
        $package["desc"] = I("post.tDesc");
        $package["price"] = $tTotalPrice;
        $package["starttime"] = I("post.tStartTime");
        $package["endtime"] = I("post.tEndTime");

        if($packageId){
            $m->where(array("id"=>$packageId))->save($package);

            if($gm->where(array("tId"=>$packageId))->delete()){
                foreach ($goodsList as $k=>$goods) {
                    $goodsData["goodsId"] = $k;
                    $goodsData["tPrice"] = $goods["tPrice"];
                    $goodsData["tId"] = $packageId;
                    $goodsData['periodId'] = $goods['periodId'];
                    if(!$gm->add($goodsData)){
                        break;
                    }
                }
                $flag = true;
                $data['msg'] = "修改成功";
                $data['status'] = 1;
            }


        }else{
            if($tId =$m->add($package)) {
                $flag = false;

                foreach ($goodsList as $k => $goods) {
                    $goodsData["goodsId"] = $k;
                    $goodsData["tPrice"] = $goods["tPrice"];
                    $goodsData["tId"] = $tId;
                    $goodsData['periodId'] = $goods['periodId'];
                    if ($gm->add($goodsData)){
                        $flag = true;
                        $data["status"] = 1;
                        $data["msg"] = "添加套餐成功";
                    }else{
                        break;
                    }
                }
            }
        }

        if($flag){
            $m->commit();
        }else{
            $m->rollback();
        }


        $this->ajaxReturn($data);


    }


    /**
     *套餐列表
     */

     public function index(){
         $packageModel = M("taocans");
         $where = array();
         if($tName = I("post.tName")){
             $where["tName"] = array("like","%$tName%");
         }

         if($shopName = I("post.shopName")){
             $where["shopName"] = array("like","%$shopName%");
         }

         if($goodsName = I("post.goodsName")){
             $where["goodsName"] = array("like","%$goodsName%");
         }

         $where["is_del"] = 0;

         $buildSql = $packageModel->join("join wst_taocan_goods on wst_taocans.id = wst_taocan_goods.tId")
             ->join("join wst_goods on wst_taocan_goods.goodsId = wst_goods.goodsId")->where($where)
             ->join("join wst_shops on wst_goods.shopId = wst_shops.shopId")
             ->field("distinct(wst_taocans.id),wst_taocans.id as tId,wst_taocans.
             price,tName")->where($where)->select(false);

         $count = $packageModel->table($buildSql.' a')->count("id");
         $page = new \Think\Page($count,5);
         $show = $page->show();
         $pList = $packageModel->join("join wst_taocan_goods on wst_taocans.id = wst_taocan_goods.tId")
             ->join("join wst_goods on wst_taocan_goods.goodsId = wst_goods.goodsId")
             ->join("join wst_shops on wst_goods.shopId = wst_shops.shopId")
             ->field("distinct(wst_taocans.id) as tId,wst_taocans.
             price,tName")
             ->where($where)->order("tId desc")->limit($page->firstRow.','.$page->listRows)->select();
         $tGoodsModel = M("TaocanGoods");
         foreach($pList as &$pItem){
             $goodsList = $tGoodsModel->join("join  wst_goods on wst_taocan_goods.goodsId = wst_goods.goodsId")
                 ->join("join wst_shops on wst_shops.shopId = wst_goods.shopId")
                 ->join("left join wst_price on priceId = wst_taocan_goods.periodId")
                 ->where(array("tId"=>$pItem["tId"]))
                 ->field("wst_goods.goodsId,goodsName,shopPrice,goodsSpec,wst_shops.shopName,goodsThums,
                 tPrice,periodId,weekDate,periodStart,periodEnd,wst_price.price as pPrice,isTom")->select();
             $pItem["goods"] = $goodsList;
         }
         $this->assign("packageList",$pList);
         $this->assign("page",$show);
         $this->display("package/index");
     }


    /**
     * 加载时段价格数据
     */

    public function getPeriodPrice($id){
        $priceList = M("Price")->where(array("goodsId"=>$id))->select();
        $weekPrice = array();
        foreach($priceList as $v){
            $weekPrice[$v["weekDate"]][] = $v;
        }

        $this->assign("goodsId",$id);
        $this->assign("monPrice",$weekPrice[1]);
        $this->assign("tuePrice",$weekPrice[2]);
        $this->assign("wedPrice",$weekPrice[3]);
        $this->assign("thuPrice",$weekPrice[4]);
        $this->assign("friPrice",$weekPrice[5]);
        $this->assign("satPrice",$weekPrice[6]);
        $this->assign("sunPrice",$weekPrice[7]);
        $this->display("package/periodPrice");
    }


    /**
     * 删除套餐
     */

    public function delPackage(){
        $data["status"] = 0;
        if(M("Taocans")->where(array("id"=>I("post.id")))->setField("is_del",1)){
            $data["status"] = 1;
        }
        $this->ajaxReturn($data);
    }
}